/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.criteria.weights;

import data.ACStorage;
import exception.MarkNotInSatScaleException;
import java.util.ArrayList;
import java.util.List;
import logic.Normalizer;
import model.Goal;

/**
 *
 * @author Djordje Gligorijevic
 */
public class ControllerCriteriaWeights {

    private ControllerCriteriaWeights() {
    }

    public static ControllerCriteriaWeights getInstance() {
        return ControllerCriteriaWeightsHolder.INSTANCE;
    }

    private static class ControllerCriteriaWeightsHolder {

        private static final ControllerCriteriaWeights INSTANCE = new ControllerCriteriaWeights();
    }

    public void normalize(String normalizer, Goal goal) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        ((Normalizer) Class.forName("logic." + normalizer).newInstance()).normalize(goal);
    }
    
    public void calculateActionPerformed () {
        List<Double> criteriaPonders = new ArrayList<>();
        for (int i = 0; i < ACStorage.getInstance().getGoal().getListCriteria().size(); i++) {
            double ponder = 0;
            for (int j = 0; j < ACStorage.getInstance().getGoal().getCriteriaWeights().size(); j++) {
                if (ACStorage.getInstance().getGoal().getCriteriaWeights().get(j).getSecondCriteria().
                        equals(ACStorage.getInstance().getGoal().getListCriteria().get(i))) {
                    ponder += ACStorage.getInstance().getGoal().getCriteriaWeights().get(j).getNormalizedMark();
                }
            }
            criteriaPonders.add(ponder / ACStorage.getInstance().getGoal().getListCriteria().size());
        }
        ACStorage.getInstance().getGoal().setCriteriaPonders(criteriaPonders);
    }
    
    public void weightCriteriasMouseReleased (int rowIndex, int columnIndex, int sliderValue) throws MarkNotInSatScaleException {
        if (sliderValue >=0) {
            ACStorage.getInstance().getGoal().addCriteriaWeight(
                            ACStorage.getInstance().getGoal().getListCriteria().get(columnIndex - 1),
                            ACStorage.getInstance().getGoal().getListCriteria().get(rowIndex),
                            Math.abs(sliderValue) + 1);
            ACStorage.getInstance().getGoal().addCriteriaWeight(
                            ACStorage.getInstance().getGoal().getListCriteria().get(rowIndex),
                            ACStorage.getInstance().getGoal().getListCriteria().get(columnIndex - 1),
                            (1.0 / (Math.abs(sliderValue) + 1)));
        } else {
            ACStorage.getInstance().getGoal().addCriteriaWeight(
                            ACStorage.getInstance().getGoal().getListCriteria().get(columnIndex - 1),
                            ACStorage.getInstance().getGoal().getListCriteria().get(rowIndex),
                            (1.0 / (Math.abs(sliderValue) + 1)));
            ACStorage.getInstance().getGoal().addCriteriaWeight(
                            ACStorage.getInstance().getGoal().getListCriteria().get(rowIndex),
                            ACStorage.getInstance().getGoal().getListCriteria().get(columnIndex - 1),
                            Math.abs(sliderValue) + 1);
        }
    }   
}
